package com.chujian.Activities;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

import com.chujian.Adapter.TableGridAdapter;
import com.chujian.Global.Global;
import com.chujian.Threads.HttpClientGetThread;
import com.chujian.Utils.Constan;
import com.chujian.Utils.JsonUtil;
import com.chujian.model.Table;
import com.chujian.orderingsystem.R;

public class TableSelectActivity extends Activity implements OnItemClickListener{
	private GridView mGridView;
	private TableGridAdapter mAdapter;
	private List<Table> mList;
	private boolean bookFlag;
	private Handler mHandler=new Handler(){
		@Override
		public void handleMessage(android.os.Message msg) {
			super.handleMessage(msg);
			String str=(String) msg.obj;
			mList=JsonUtil.TableListFromJson(str);
			mAdapter=new TableGridAdapter(mList, TableSelectActivity.this);
			mGridView.setAdapter(mAdapter);
		}
		};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.table_select);
		mGridView=(GridView) findViewById(R.id.gv_table_select);
		mGridView.setOnItemClickListener(this);
		
		Intent intent=getIntent();
		bookFlag=intent.getBooleanExtra("bookFlag", false);
		
		
		String url=Constan.BASE_URL+"TableListServlet";
		new HttpClientGetThread(url, mHandler).start();;
	}
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		if(bookFlag){
			Intent data=new Intent();
			data.putExtra("tableName", mList.get(position).getTableName());
			setResult(1002, data);
			TableSelectActivity.this.finish();
		}else{
			Intent intent =new Intent(TableSelectActivity.this, TakeOrderActivity.class);
			Global.selectedTable=mList.get(position).getTableName();
			startActivity(intent);
		}
		
	}
	
	public void IbBack(View v){
		finish();
	}

}
